package com.yy.web.pm.shopPeople.service.Impl;

import com.yy.web.pm.alarm.domain.AlarmDate;
import com.yy.web.pm.alarm.mapper.AlarmDateMapper;
import com.yy.web.pm.billboard.domain.BillboardData;
import com.yy.web.pm.billboard.mapper.BillboardDataMapper;
import com.yy.web.pm.billboard.service.BillboardDataService;
import com.yy.web.pm.billboardRemove.domain.BillboardRemove;
import com.yy.web.pm.billboardRemove.mapper.BillboardRemoveMapper;
import com.yy.web.pm.patrol.domain.PeoplePatrol;
import com.yy.web.pm.patrol.mapper.PeoplePatrolMapper;
import com.yy.web.pm.shopPeople.Dto.ShopDto;
import com.yy.web.pm.until.CodeGenerator;
import com.yy.web.pm.user.domain.User;
import com.yy.web.pm.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.yy.web.pm.shopPeople.mapper.ShopPeopleMapper;

import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.yy.web.pm.shopPeople.domain.ShopPeople;
import com.yy.web.pm.shopPeople.service.ShopPeopleService;

@Service
public class ShopPeopleServiceImpl implements ShopPeopleService {

    @Resource
    private ShopPeopleMapper shopPeopleMapper;
    @Autowired
    private PeoplePatrolMapper peoplePatrolMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AlarmDateMapper alarmDateMapper;
    @Autowired
    private BillboardDataMapper billboardDataMapper;
    @Autowired
    private BillboardRemoveMapper billboardRemoveMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return shopPeopleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ShopPeople record) {
        return shopPeopleMapper.insert(record);
    }

    @Override
    public int insertSelective(ShopPeople record) {
        return shopPeopleMapper.insertSelective(record);
    }

    @Override
    public ShopPeople selectByPrimaryKey(Integer id) {
        return shopPeopleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ShopPeople record) {
        //是否需要整改0:存在隐患 1不需要 2:需要 3:拆除
        if (record.getIsjudge().equals("2")) {
            shopPeopleMapper.updateByPrimaryKeySelective(record);
            //添加巡检任务
            addPartol(record, peoplePatrolMapper, "隐患整改");
        } else if (record.getIsjudge().equals("3")) {
            // shopPeopleMapper.updateByPrimaryKeySelective(record);
            // //添加拆除任务
            // addPartol(record, peoplePatrolMapper, "拆除");
        }
        return shopPeopleMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * @param record:
     * @param peoplePatrolMapper:
     * @Title:addPartol
     * @Description: 方法描述:商家整改后向对应的负责人进行发送巡检通知
     */

    private void addPartol(ShopPeople record, PeoplePatrolMapper peoplePatrolMapper, String name) {
        User user = userMapper.selectByPrimaryKey(Long.valueOf(record.getUserId()));
        PeoplePatrol peoplePatrol = new PeoplePatrol()
                .setUserId(Math.toIntExact(user.getId()))
                .setPatrolPeople(user.getUsername())
                .setPartrolStore(record.getShopName())
                .setPartrolAddress(record.getShopAdress())
                .setPartrolType(name)
                .setPartrolNumber(CodeGenerator.generateOrderNo())//巡检编号
                .setPartrolProgress("0")
                .setPartrolNotes(record.getChangeNotes()).setPartrolLevel("1")//高级巡检
                .setPartrolTime(new Date());
        peoplePatrolMapper.insert(peoplePatrol);

    }

    @Override
    public int updateByPrimaryKey(ShopPeople record) {
        return shopPeopleMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<ShopPeople> list) {
        return shopPeopleMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<ShopPeople> list) {
        return shopPeopleMapper.batchInsert(list);
    }

    @Override
    public List<ShopPeople> selectList(ShopPeople shopPeople) {
        return shopPeopleMapper.selectList(shopPeople);
    }

    @Override
    public int insertAnBillboard(ShopDto shopDto) {
        //新增店铺
        ShopPeople shopPeople = new ShopPeople()
                .setShopName(shopDto.getShopName()).setShopAdress(shopDto.getShopAdress())
                .setAdvertisementMaterial(shopDto.getAdvertisementMaterial())
                .setAdvertisementSize(shopDto.getAdvertisementSize())
                .setAdvertisementLongitude(shopDto.getAdvertisementLongitude())
                .setAdvertisementDimension(shopDto.getAdvertisementDimension())
                .setAdvertisementId(shopDto.getAdvertisementId())
                .setAdvertisementFloor(shopDto.getAdvertisementFloor())
                .setShopApplyTime(shopDto.getShopApplyTime())
                .setExamineProgress("1")//新增店铺默认为1
                .setAdvertisementWide(shopDto.getAdvertisementWide()).setAdvertisementTall(shopDto.getAdvertisementTall())
                .setAdvertisementAdress(shopDto.getAdvertisementAdress());
        shopPeopleMapper.insert(shopPeople);

        //图片处理
        List<String> image = shopDto.getImage();
        String string = image.stream().filter(p -> !p.isEmpty() && p != null).collect(Collectors.joining(","));

        //新增广告牌
        BillboardData billboard = new BillboardData().setShopId(String.valueOf(shopPeople.getId())).setAdvertisementTestTime(shopDto.getAdvertisementTestTime())
                .setAdvertisementStarttime(shopDto.getAdvertisementStarttime()).setAdvertisementEndtime(shopDto.getAdvertisementEndtime())
                .setAdvertisementResult(shopDto.getAdvertisementResult()).setDetectionPeople(shopDto.getDetectionPeople()).setDetectionImage(string)
                .setAdvertisementNumber(shopDto.getAdvertisementNumber()).setAdvertisementName(shopDto.getAdvertisementName()).setAdvertisementAddress(shopDto.getAdvertisementAddress());
        billboardDataMapper.insert(billboard);
        return 0;
    }

}
